package com.coolgor.axnut.service.lordsee.impl;

import com.coolgor.axnut.dao.lordsee.ScanSegmentDao;
import com.coolgor.axnut.dto.lordsee.ScanSegmentExecution;
import com.coolgor.axnut.entity.lordsee.ScanSegment;
import com.coolgor.axnut.enums.lordsee.ScanSegmentOperationStateEnum;
import com.coolgor.axnut.exceptions.lordsee.ScanSegmentOperationException;
import com.coolgor.axnut.service.lordsee.ScanSegmentService;
import com.coolgor.axnut.util.PageCalculator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class ScanSegmentServiceImpl implements ScanSegmentService {
    private static Logger log = LoggerFactory.getLogger(ScanSegmentServiceImpl.class);

    @Autowired
    private ScanSegmentDao scanSegmentDao;

    @Override
    public ScanSegment getScanSegment(Long recordID) {
        return scanSegmentDao.queryScanSegmentById(recordID);
    }

    @Override
    public ScanSegmentExecution getScanSegmentList(ScanSegment scanSegmentCondition, int pageIndex, int pageSize) {
        //将页码转换成行码
        int rowIndex = PageCalculator.calculateRowIndex(pageIndex, pageSize);
        //依据查询条件，调用dao层返回相关的查询结果列表
        List<ScanSegment> list = scanSegmentDao.queryScanSegmentList(scanSegmentCondition, rowIndex, pageSize);
        //依据相同的查询条件，返回查询结果总数
        int count = scanSegmentDao.queryScanSegmentCount(scanSegmentCondition);
        ScanSegmentExecution result = new ScanSegmentExecution();
        if (null != list) {
            result.setState(ScanSegmentOperationStateEnum.SUCCESS.getState());
            result.setStateInfo(ScanSegmentOperationStateEnum.SUCCESS.getStateInfo());
            result.setList(list);
            result.setCount(count);
        } else {
            result.setState(ScanSegmentOperationStateEnum.INNER_ERROR.getState());
            result.setStateInfo(ScanSegmentOperationStateEnum.INNER_ERROR.getStateInfo());
        }
        return result;
    }

    @Override
    @Transactional
    public ScanSegmentExecution addScanSegment(ScanSegment scanSegment) throws ScanSegmentOperationException {
        // 空值判断
        if (scanSegment == null) {
            return new ScanSegmentExecution(ScanSegmentOperationStateEnum.NULL);
        }
        int effectedNum;
        try {
            // 添加信息
            effectedNum = scanSegmentDao.insertScanSegment(scanSegment);
            if (effectedNum <= 0) {
                throw new ScanSegmentOperationException("扫描网段信息插入失败");
            }
        } catch (Exception e) {
            throw new ScanSegmentOperationException("Add ScanSegment error:" + e.getMessage());
        }
        return new ScanSegmentExecution(ScanSegmentOperationStateEnum.SUCCESS,scanSegment);
    }

    @Override
    @Transactional
    public ScanSegmentExecution modifyScanSegment(ScanSegment scanSegment) throws ScanSegmentOperationException{
        // 空值判断
        if (scanSegment == null) {
            return new ScanSegmentExecution(ScanSegmentOperationStateEnum.NULL);
        }
        int effectedNum;
        try {
            // 更新信息
            effectedNum = scanSegmentDao.updateScanSegment(scanSegment);
            if (effectedNum <= 0) {
                throw new ScanSegmentOperationException("扫描网段信息更新失败");
            }
        } catch (Exception e) {
            throw new ScanSegmentOperationException("Modify ScanSegment error:" + e.getMessage());
        }
        return new ScanSegmentExecution(ScanSegmentOperationStateEnum.SUCCESS,scanSegment);
    }

    @Override
    @Transactional
    public ScanSegmentExecution removeScanSegment(ScanSegment scanSegment) throws ScanSegmentOperationException{
        // 空值判断
        if (scanSegment == null) {
            return new ScanSegmentExecution(ScanSegmentOperationStateEnum.NULL);
        }
        int effectedNum;
        try {
            // 执行删除操作
            effectedNum = scanSegmentDao.deleteScanSegment(scanSegment);
            if (effectedNum <= 0) {
                throw new ScanSegmentOperationException("扫描网段信息删除失败");
            }
        } catch (Exception e) {
            throw new ScanSegmentOperationException("Remove ScanSegment error:" + e.getMessage());
        }
        return new ScanSegmentExecution(ScanSegmentOperationStateEnum.SUCCESS,scanSegment);
    }

    @Override
    @Transactional
    public ScanSegmentExecution batchRemoveScanSegment(List<ScanSegment> list) throws ScanSegmentOperationException {
        // 空值判断
        if (list == null) {
            return new ScanSegmentExecution(ScanSegmentOperationStateEnum.NULL);
        }
        List<Long> recordIDList = new ArrayList<Long>();
        for (int i=0; i<list.size(); i++) {
            recordIDList.add(list.get(i).getRecordID());
        }
        int effectedNum;
        try {
            // 执行删除操作
            effectedNum = scanSegmentDao.batchDeleteScanSegment(recordIDList);
            if (effectedNum <= 0) {
                throw new ScanSegmentOperationException("扫描网段信息批量删除失败");
            }
        } catch (Exception e) {
            throw new ScanSegmentOperationException("Batch Delete ScanSegment error:" + e.getMessage());
        }
        ScanSegmentExecution result = new ScanSegmentExecution(ScanSegmentOperationStateEnum.SUCCESS);
        result.setCount(effectedNum);
        return  result;
    }
}
